package org.jadapter.tests;

import java.util.List;

import org.jadapter.JAdapter;
import org.jadapter.tests.examples.CommaSeparated;
import org.jadapter.tests.examples.CommaSeparatedSubclass;

@SuppressWarnings("unchecked")
public class TestNamedComponents extends BaseTestCase {

    JAdapter<CommaSeparated,List> myAdapter;
    
    @Override
	public void setUp() {
        super.setUp();

        myAdapter = 
            new JAdapter<CommaSeparated,List>();
        myAdapter.setFor(List.class);
        myAdapter.setProvides(CommaSeparated.class);
        myAdapter.setAdapter(CommaSeparatedSubclass.class);
        
        // set the name we are interested in
        myAdapter.setName("myAdapter");
    }
    
    public void testShouldNotReturnNamedAdapter(){
        registry.register(myAdapter);
    	registry.register(listAdapter);        
        
        CommaSeparated cs = registry.adapt(CommaSeparated.class, list);
        assertEquals("comma separated list", "Foo,Bar", cs.getCommaSeparated());
        assertFalse(cs instanceof CommaSeparatedSubclass);    	
    }
    
    public void testShouldReturnNamedAdapter() {
        registry.register(listAdapter);
        registry.register(myAdapter);
        
        CommaSeparated cs = registry.adapt(CommaSeparated.class, list, "myAdapter");
        assertEquals("comma separated list", "Foo,Bar", cs.getCommaSeparated());
        assertTrue(cs instanceof CommaSeparatedSubclass);        
    }    
    
    public void testShouldNotReturnUnknownNamedAdapter() {
        registry.register(listAdapter);
        registry.register(myAdapter);
        
        CommaSeparated cs = registry.adapt(CommaSeparated.class, list, "notAdapter");
        assertNull(cs);
    }     
}
